<?php

Yii::import('application.models._base.BaseUsers');

class Users extends BaseUsers
{
    public $userRole;
    public static function model($className=__CLASS__) {
		return parent::model($className);
	}
         public function rules() {
        return array(
            array('username, password', 'required'),
            array('createtime, lastvisit, superuser, status', 'numerical', 'integerOnly' => true),
            array('username', 'unique', 'message' => Yii::t("UserModule.user", "Username already exists.")),
            array('username', 'length', 'max' => 50),
            array('restaurantAdmins', 'safe'),
            array('password, activationKey', 'length', 'max' => 128),
            array('activationKey, createtime,lastvisit, superuser, status', 'default', 'setOnEmpty' => true, 'value' => null),
            array('id, username, password, activationKey, createtime, lastvisit, superuser, status', 'safe', 'on' => 'search'),
        );
    }

    public function validatePassword($password) {
        //echo $this->password;

        if ($this->password === md5(trim($password))) {

            return true;
        }
        return false;
    }

    public function beforeDelete() {
        Profiles::model()->deleteAll('users_id=:users_id', array(':users_id' => $this->id));
        UserHasRole::model()->deleteAll('users_id=:users_id', array(':users_id' => $this->id));

        
//        $this->profiles->delete();
//        $this->userHasRoles->delete();
//        $this->userPreferences->delete();
//        $this->reviews->deleteAll();
//        $this->authKeys->deleteAll();

        return true;
    }

    public function beforeSave() {
        parent::beforeSave();

        if ($this->isNewRecord) {
            $this->setAttribute('createtime', time());
        }
        return true;
    }

    public function deactivate() {
        $this->setAttribute('status', 0);
        $this->save();
    }

    public function addProfile($attributes) {
        $profile = new Profile();
        $profile->setAttribute('users_id', $this->id);
        $profile->setAttribute('timestamp', time());
        $profile->setAttribute('firstname', (isset($attributes['firstname']) ? $attributes['firstname'] : ''));
        $profile->setAttribute('lastname', (isset($attributes['lastname']) ? $attributes['lastname'] : ''));
        $profile->setAttribute('email',(isset($attributes['email']) ? $attributes['email'] : ''));
        $profile->setAttribute('gender',(isset($attributes['gender']) ? $attributes['gender'] : ''));
       
        $profile->save(false);
    }

    
    public function editProfile() {
        if (is_array($this->profiles)) {
            $profile = isset($this->profiles[0]) ? $this->profiles[0] : new Profiles();
        } else {
            $profile = isset($this->profiles) ? $this->profiles : new Profiles();
        }

        $profile->setAttributes($_POST['Profiles']);
        if ($profile->isNewRecord) {
            $profile->setAttribute('user_id', $this->id);
        }
        $profile->setAttribute('timestamp', time());
        $profile->save();
    }

    public function addUserRole($role_id) {
        $userRole = new UserHasRole();
        $userRole->setAttribute('users_id', $this->id);
        $userRole->setAttribute('roles_id', $role_id);
        $userRole->save(false);
    }
}